﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using MS.InformationManagement.DAL.DocumentCenter;

namespace MS.InformationManagement.BLL.GestionDeUsuarios
{
    public abstract class BaseUserPermissionsCommand
    {

        #region Protected Properties

        protected string _targetLoginUser;
        protected Roles _role;

        #endregion

        #region Constructors

        public BaseUserPermissionsCommand(string targetLoginUser, Roles role)
        {
            _targetLoginUser = targetLoginUser;
            _role = role;
        }

        #endregion

        #region private methods

        protected static SPUser getRoleUser(string targetUser)
        {
            var currentUser = (from user in SPContext.Current.Web.SiteUsers.OfType<SPUser>()
                               where user.LoginName == targetUser
                               select user).FirstOrDefault();

            if (targetUser == null)
                throw new Exception(string.Format("No existe un usuario {0} en el sitio", targetUser));

            return currentUser;
        }

        protected static SPGroup getRoleGroup(Roles role)
        {

            var group = (from gp in SPContext.Current.Web.Groups.OfType<SPGroup>()
                         where gp.ID == role.GrupoId
                         select gp).FirstOrDefault();

            if (group == null)
                throw new Exception(string.Format("No se existe el grupo asociado al role {0}", role.Grupo));

            return group;
        }

        #endregion

    }
}
